package com.hainatong.jcmvvmbase.login.viewmodel;


import java.util.List;

import com.hainatong.jcmvvmbase.Constants;
import com.hainatong.jcmvvmbase.api.ApiService;
import com.hainatong.jcmvvmbase.login.entity.GirlImgBean;
import com.hainatong.jcmvvmbase.login.entity.GirlsResult;
import com.hainatong.jcmvvmbase.login.entity.LoginResult;
import com.hainatong.jcmvvmbase.login.entity.QueryGirls;
import com.hainatong.jcmvvmbase.repository.DataRepository;
import com.hainatong.jcmvvmbase.utils.MD5;
import com.hainatong.mvvmbase.base.BaseViewModel;
import com.hainatong.mvvmbase.http.RxSchedulersUtils;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


public class LoginViewModel extends BaseViewModel
{
    // Xml 布局文件中 @{ viewmodel.username } 是单向绑定
    // @={ viewmodel.username } 是双向绑定
    public final ObservableField<String> username = new ObservableField<>();

    public final ObservableField<String> pwd = new ObservableField<>();

    private int page;

    private boolean isRefresh = false;

    private boolean isHasNextPage = false;

    public MutableLiveData<QueryGirls> mQueryResult = new MutableLiveData<>();

    public LoginViewModel(@NonNull Application application)
    {
        super(application);
    }

    public MutableLiveData<LoginResult> login()
    {
        return DataRepository.getInstance().getRepository().login(this.username.get(),
            MD5.encode(this.pwd.get() + Constants.SIGN_STR));
    }

    public MutableLiveData<List<GirlImgBean>> getImgs()
    {
        MutableLiveData<List<GirlImgBean>> mResult = new MutableLiveData<>();

        ApiService.Builder.getGankIoServer().getGankIoData("Girl", "Girl", 1, 20).compose(
            RxSchedulersUtils.Obs_io_main()).subscribe(new Observer<GirlsResult>()
            {
                @Override
                public void onSubscribe(Disposable d)
                {
                    addDisposable(d);
                }

                @Override
                public void onNext(GirlsResult result)
                {
                    if (result != null && result.getData() != null)
                    {
                        mResult.postValue(result.getData());
                    }
                }

                @Override
                public void onError(Throwable e)
                {}

                @Override
                public void onComplete()
                {}
            });

        return mResult;
    }

    public MutableLiveData<QueryGirls> queryGirlImgs()
    {
        this.isRefresh = true;
        this.page = 1;
        return queryGirlImgsByPage(page);
    }

    private MutableLiveData<QueryGirls> queryGirlImgsByPage(int mPage)
    {
        this.page = mPage;
        ApiService.Builder.getGankIoServer().getGankIoData("Girl", "Girl", page, 20).compose(
            RxSchedulersUtils.Obs_io_main()).subscribe(new Observer<GirlsResult>()
            {
                @Override
                public void onSubscribe(Disposable d)
                {
                    addDisposable(d);
                }

                @Override
                public void onNext(GirlsResult result)
                {
                    QueryGirls queryGirls = new QueryGirls();
                    queryGirls.setRefresh(isRefresh);

                    if (result != null && result.getData() != null && !result.getData().isEmpty())
                    {
                        isHasNextPage = true;
                        page = page + 1;
                        queryGirls.setGirlImgs(result.getData());

                    }
                    else
                    {
                        isHasNextPage = false;
                        if (!isRefresh)
                        {
                            queryGirls.setLoadMoreEmpty(true);
                        }
                        queryGirls.setGirlImgs(null);
                    }

                    mQueryResult.postValue(queryGirls);
                }

                @Override
                public void onError(Throwable e)
                {
                    QueryGirls qGirls = new QueryGirls();
                    qGirls.setRefresh(isRefresh);
                    qGirls.setGirlImgs(null);
                    if (!isRefresh)
                    {
                        qGirls.setLoadMoreEmpty(true);
                    }
                    mQueryResult.postValue(qGirls);
                }

                @Override
                public void onComplete()
                {}
            });

        return mQueryResult;
    }

    public void loadMoreGirls()
    {
        this.isRefresh = false;
        if (isHasNextPage)
        {
            queryGirlImgsByPage(this.page);
        }
        else
        {
            QueryGirls qGirls = new QueryGirls();
            qGirls.setRefresh(isRefresh);
            qGirls.setGirlImgs(null);
            if (!isRefresh)
            {
                qGirls.setLoadMoreEmpty(true);
            }
            mQueryResult.postValue(qGirls);
        }

    }

    @Override
    protected void onCleared()
    {
        super.onCleared();
        DataRepository.getInstance().getRepository().unDisposable();
    }
}
